<?php
require_once $_SERVER ['DOCUMENT_ROOT']."/bloodPet/application/config/control.php";
include_once $_SERVER ['DOCUMENT_ROOT'].BASE_PROJECT_PATH.'/application/model/LoginCheckM.php';
include_once $_SERVER ['DOCUMENT_ROOT'].BASE_PROJECT_PATH.'/application/model/LogUserActionM.php';
 
abstract class BaseS{
	
	public 	$log;
	private $tragetObj;
	
	function __construct() {
		header("Content-Type:text/html; charset=utf-8");
	}
	
	public function getRequest($name) {
		return isset($_REQUEST[$name]) ?  $_REQUEST[$name] : null;
	}
	
	function setTarget($obj) {
		$this->tragetObj = $obj;
	}
	
	public function __call($name, $args) {
		@session_start();
		$checkR = $this->checkLogin($name);
		
		if($checkR){
			$r = new ReflectionClass($this-> tragetObj);
			if ($method = $r->getMethod($name)) {
				return $method->invoke($this-> tragetObj, $args);
			}
		}else{
			echo "{\"sl\":\"0\"}";
			exit;
		}
		
	}
	
	public function changePhotoPath($photo){
		return str_replace(BASE_PHOTO_PATH,"",$photo);
	}
	
	public function checkLogin($name){
		
		$dao = new LoginCheckM();
		if($name == "logout"){
			return true;
		}else{
			
			$result = true;
			if(isset($_SESSION['user']) && @$_SESSION['user']->live_key != ""){
				$r = $dao->checkUser($_SESSION['user']->account, $_SESSION['user']->live_key);
				if(!$r){
					unset($_SESSION['user']);
					$result = false;
				}
			}
			
			if(isset($_SESSION['member']) && @$_SESSION['member']->live_key != "" ){
				$r = $dao->checkCompany($_SESSION['member']->account, $_SESSION['member']->live_key);
				if(!$r){
					unset($_SESSION['member']);
					$result = false;
				}
			}
			
			return $result;
		}
		
	} 
	
	public function logUserAction($ip, $actionArea, $actionType){
		
		$user = $_SESSION['user'];
		$dao = new LogUserActionM();
		$obj = array(
				'user_id' 		=> $user->id,
				'ip' 			=> $ip,
				'action_area' 	=> $actionArea,
				'action_type'	=> $actionType
				);
		$dao->insert($obj);
	}
	

	
}	
?>